package com.logansoft.servicegateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author liqiye
 * @description gateway的自定义全局过滤器
 * gateway的过滤器只会过滤路由中的接口，这个服务webFlux的接口不会过这些过滤器
 * @date 2021/07/16
 */

@Component
@Slf4j
public class GatewayGlobalFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        // log.info("gateway全局filter");

        // 全局过滤获取 token
        // String token = exchange.getRequest().getQueryParams().getFirst("token");
        // if(token == null || token.equals("")){
        //     log.info("token为空");
        //     exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
        //     return exchange.getResponse().setComplete();
        // }else{
        //     // 验证 token
        // }
        return chain.filter(exchange);
    }

    // 过滤器执行的顺序
    @Override
    public int getOrder() {
        return 1;
    }

}
